package top.zhaoyuliu.logbook.client.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.intf.ClientAioHandler;
import org.tio.client.intf.ClientAioListener;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import top.zhaoyuliu.logbook.client.appender.LogAppenderInit;
import top.zhaoyuliu.logbook.client.compent.AppenderCompent;
import top.zhaoyuliu.logbook.client.connect.client.ClientServerStart;
import top.zhaoyuliu.logbook.client.connect.client.LbClientAioHandler;
import top.zhaoyuliu.logbook.client.connect.client.LbClientAioListener;
import top.zhaoyuliu.logbook.client.properties.LogbookClientProperties;
import top.zhaoyuliu.logbook.client.utils.ApplicationContextProvider;

/**
 * Copyright © 2021 Arain. All rights reserved.
 *
 * @Title: ClientConfig.java
 * @Prject: logbook-client
 * @Package: top.zhaoyuliu.logbook.client.config
 * @Description: <功能详细描述>
 * @author: Arain
 * @date: 2021年1月22日 下午1:11:25
 * @version: V1.0
 */
@Configuration
@EnableConfigurationProperties(LogbookClientProperties.class)
@ComponentScan("top.zhaoyuliu.logbook.client.*")
@ServletComponentScan(basePackages = { "top.zhaoyuliu.logbook.client.filter" })
@Order(0)
public class AutoLbClientConfig {

	@Value("${server.port}")
	private Integer serverPort;

	@Bean
	public ClientGroupContext clientGroupContext(LogbookClientProperties logbookClientProperties) {
		// handler, 包括编码、解码、消息处理
		ClientAioHandler clientAioHandler = new LbClientAioHandler();
		// 事件监听器，可以为null，但建议自己实现该接口
		ClientAioListener clientAioListener = new LbClientAioListener();
		// 断链后自动连接的，不想自动连接请设为null
		ReconnConf reconnConf = new ReconnConf(5000L, 0);
		ClientGroupContext clientGroupContext = new ClientGroupContext(clientAioHandler, clientAioListener, reconnConf);
		clientGroupContext.setHeartbeatTimeout(0);
		return clientGroupContext;
	}

	@Bean
	public ClientServerStart clientServerStart(ClientGroupContext clientGroupContext,
			LogbookClientProperties logbookClientProperties) {
		ClientServerStart clientServerStart = new ClientServerStart(clientGroupContext, logbookClientProperties);
		if (ObjectUtil.isNotNull(serverPort)) {
			AppenderCompent.setPort(serverPort);
		}
		if (CollectionUtil.isNotEmpty(logbookClientProperties.getLevel())) {
			AppenderCompent.level = logbookClientProperties.getLevel();
		}
		clientServerStart.start();
		return clientServerStart;
	}

	@Bean
	public ApplicationContextProvider applicationContextProvider() {
		return new ApplicationContextProvider();
	}

	@Bean
	public LogAppenderInit appenderInit() {
		return new LogAppenderInit();

	}
}
